package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.BannerBO;
import io.github.talelin.latticy.dto.BannerWithItemsDTO;
import io.github.talelin.latticy.mapper.BannerItemMapper;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.Banner;
import io.github.talelin.latticy.model.BannerItem;
import io.github.talelin.latticy.service.BannerService;
import io.github.talelin.latticy.common.util.BeanCopyUtil;
import io.github.talelin.latticy.common.mybatis.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * banner业务层
 *
 * @author YiHua
 * @date 2021/11/25 4:16 下午
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    /*
     * this 能指代 mapper
     * 例如本类的: this ——> BannerMapper
     */

    @Autowired
    private BannerItemMapper bannerItemMapper;       // bannerItem数据库层

    /**
     * 查询多个banner,支持分页
     *
     * @param pageNum  false 当前页
     * @param pageSize false 每页条数
     */
    @Override
    public IPage<Banner> paging(Integer pageNum, Integer pageSize) {
        // 设置分页参数
        Page<Banner> page = new Page<>(pageNum, pageSize);
        // 返回分页后的banner数据集合
        // FIXME 这个方法其实，接口层已经继承，可以在控制层直接写，但是控制层出现mapper不好
        return this.getBaseMapper().selectPage(page, null);
    }

    /**
     * 查询banner信息包含子项信息
     *
     * @param id bannerId
     */
    @Override
    public BannerWithItemsDTO getWithItems(Integer id) {
        // 根据id查询banner信息
//        Banner banner = this.getById(id);

        // 判断banner是否存在
//        if (banner == null) {
//            throw new NotFoundException(20000);
//        }

//              // 最基础的查询写法
//              QueryWrapper<BannerItem> wrapper = new QueryWrapper<>();
//              wrapper.eq("banner_id", id);
//              List<BannerItem> bannerItems = this.bannerItemMapper.selectList(wrapper);

//              // lambda表达式的写法
//              LambdaQueryWrapper<BannerItem> wrapper = new LambdaQueryWrapper<>();
//              wrapper.eq(BannerItem::getBannerId, id);
//              List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);

        // 链式调用写法
//        List<BannerItem> bannerItems = new LambdaQueryChainWrapper<>(bannerItemMapper)
//            .eq(BannerItem::getBannerId, id)
//            .list();

        return this.baseMapper.getWithItems(id);

        // 返回完整的banner信息
//        return new BannerWithItemsDTO(banner, bannerItems);
    }

    /**
     * 删除banner
     *
     * @param id bannerId
     */
    @Override
    public void delete(Integer id) {
        Banner banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        this.getBaseMapper().deleteById(id);
    }

    /**
     * 修改banner
     *
     * @param bannerBO banner的入参信息
     * @param id       bannerId
     */
    @Override
    public void update(BannerBO bannerBO, Integer id) {
        Banner banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        BeanCopyUtil.copy(bannerBO, banner);
        this.updateById(banner);
    }
}
